<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
    
	<script type="text/javascript">
		//用户参数列表数据源
		var code_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields: ["codetype","codename","codetypeid"],
    		url: "systemUserCode!selectSystemUserCode.action"
    	});
    	
			//定义数据显示面板
			var p1 = {
				id: "p1",
				xtype: "grid",
				stripeRows:true,
				loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
				autoExpandColumn: "codename",
				autoExpandMin : 100,//自动填充最小宽度
				store: code_store,
				headersDisabled: true,
				columns:[
	    	    	new Ext.grid.RowNumberer({header: "序号", width: 35}),
    					{header: "编号",	dataIndex: "codetype", menuDisabled: true},
    					{header: "名称",	dataIndex: "codename", menuDisabled: true,id: "codename"},
    					{header: "类别",	dataIndex: "codetypeid", menuDisabled: true}
				],
				buttonAlign: "center",
				bbar: new Ext.ux.PagingToolbar({
    	        	pageSize: limit,
    	        	store: code_store
    	    	}),
				buttons:[{
					text:"新增", 
					handler: function(){
						oper_type = "add";
						Ext.getCmp("p2").resetParam();
						Ext.getCmp("mycontainer").layout.setActiveItem(1);
					}
				},{
					text:"修改", 
					handler: function(){
						var record = Ext.getCmp("p1").getSelectionModel().getSelected();
						if (record){
							Ext.getCmp("p2").setParam(record.data);
							oper_type = "upd";
							Ext.getCmp("mycontainer").layout.setActiveItem(1);
						} else {
							//Ext.Msg.alert("警告","请先选择一条记录，然后点击修改");
								Ext.Msg.show({
		                        title:"警告",
		                        msg: "请先选择一条记录，然后点击修改!",
		                        buttons: Ext.Msg.OK,
		                        animEl: "elId",
		                        icon:Ext.MessageBox.WARNING
		                        });
						}
					}
				},{
					text:"删除", 
					handler: function(){
						var record = Ext.getCmp("p1").getSelectionModel().getSelected();
						if(record){
							Ext.Msg.confirm("警告","确定要删除吗?",function(button) {
								if(button == "yes") {
									var url = "systemUserCode!deleteSystemUserCode.action";
									var params = {
										"systemUserCode.codetype":record.data.codetype,
										"systemUserCode.codename":record.data.codename,
										"systemUserCode.codetypeid":record.data.codetypeid
									};
									oper(url,params);
								}
							});
						} else {
							//Ext.Msg.alert("警告","请先选择一条记录，然后点击删除!");
							Ext.Msg.show({
		                        title:"警告",
		                        msg: "请先选择一条记录，然后点击删除!!",
		                        buttons: Ext.Msg.OK,
		                        animEl: "elId",
		                        icon:Ext.MessageBox.WARNING
		                        });
						}
				}}]
			};
			var p2 = {
				id: "p2",
			    labelWidth: 35,
				xtype: "form",
			    bodyStyle:"padding:5px 5px 0",
				defaultType: "textfield",
				items: [{
					id: "systemUserCode.codetype", 
					fieldLabel: "编号",	
					allowBlank: false,
					name: "systemUserCode.codetype",
					anchor: "100%",
					vtype:"alphanum",
					maxLength:1
				},{
					id: "systemUserCode.codename", 
					fieldLabel: "名称",	
					allowBlank: false,
					name: "systemUserCode.codename",
					anchor: "100%",
					vtype:"alphanumchinese",
					validator:function (val){
		        		if(val=="")return "该输入项为必输项！";
		        		if(Ext.fucCheckCHLength(val)>50){
		        			return "请输入50个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
		        		}
		        		return true;
		        	}
				},{
					id: "systemUserCode.codetypeid", 	  
					fieldLabel: "类别",	
					vtype:"alphanumchinese",
					name: "systemUserCode.codetypeid",
					anchor: "100%",
					validator:function (val){
		        		if(val=="")return "该输入项为必输项！";
		        		if(Ext.fucCheckCHLength(val)>20){
		        			return "请输入20个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
		        		}
		        		return true;
		        	}
				}],
				buttonAlign: "center",
				buttons:[{text:"保存",id:"save", handler: function(e){
					if(!Ext.getCmp("p2").getForm().isValid()){
						//Ext.Msg.alert("提示","验证不通过！");
							Ext.Msg.show({
		                       title:"提示",
		                       msg:"验证不通过!",
		                       buttons:Ext.Msg.OK,
		                       icon: Ext.MessageBox.ERROR
		                     });
						return;
					}
					e.setDisabled(true);
					var codetype = Ext.getCmp("systemUserCode.codetype").getValue();
					var codename = Ext.getCmp("systemUserCode.codename").getValue();
					var codetypeid = Ext.getCmp("systemUserCode.codetypeid").getValue();
					var url,params;
					if(oper_type == "add") {
						url="systemUserCode!insertSystemUserCode.action";
					} else if(oper_type == "upd") {
						url="systemUserCode!updateSystemUserCode.action";
					}
					params = {
						"systemUserCode.codetype": codetype,
						"systemUserCode.codename": codename,
						"systemUserCode.codetypeid": codetypeid
					};
					oper(url,params);
				}},{text:"取消", handler: function(){
					Ext.getCmp("mycontainer").layout.setActiveItem(0);
				}
			}],
			setParam: function(data){
				Ext.getCmp("systemUserCode.codetype").setValue(data.codetype);
				Ext.getCmp("systemUserCode.codetype").setDisabled(true);
				Ext.getCmp("systemUserCode.codename").setValue(data.codename);
				Ext.getCmp("systemUserCode.codetypeid").setValue(data.codetypeid);
				Ext.getCmp("systemUserCode.codetypeid").setDisabled(true);
			},
			resetParam: function(){
				Ext.getCmp("systemUserCode.codetype").setValue("");
				Ext.getCmp("systemUserCode.codetype").setDisabled(false);
				Ext.getCmp("systemUserCode.codename").setValue("");
				Ext.getCmp("systemUserCode.codetypeid").setValue("");
				Ext.getCmp("systemUserCode.codetypeid").setDisabled(false);
			}
		};

		var usercode_pel = new Ext.Panel({
			id: "mycontainer", 
			border : false,		
			layout: "card",
			activeItem: 0,
			items: [p1, p2]
		});
		
		code_store.load({params:{start:0,limit:limit}});
		
		var oper = function(url, params){
			Ext.Ajax.request({
				clientValidation: true,
				params: params,
				url: url,
				success: function(response, opts){
					var d=eval("("+response.responseText+")");
					if(d.code == "1"){
						Ext.getCmp("p1").getBottomToolbar().doRefresh();						
						Ext.Msg.show({
	                        title:"提示",
	                        msg:"操作成功!",
	                        buttons:Ext.Msg.OK,
	                        icon: Ext.MessageBox.INFO
	                      });
						Ext.getCmp("mycontainer").layout.setActiveItem(0);
					} else {						
						Ext.Msg.show({
	                        title:"提示",
	                        msg:"操作失败!",
	                        buttons:Ext.Msg.OK,
	                        icon: Ext.MessageBox.ERROR
	                     });
					}
	                   
	                Ext.getCmp("save").setDisabled(false);
				}
			});
		};
		
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(usercode_pel);
	CenterPanel.doLayout();
	</script>
